package com.judian.smart.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.judian.smart.R;
import com.judian.smart.util.TtsUtils;
import com.judian.smart.view.ClockView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class AlarmClockActivity extends AppCompatActivity {
    private ClockView mClockView;
    TtsUtils mTTs = new TtsUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_clock);
        mClockView = findViewById(R.id.clockView);
        TextView mainText = findViewById(R.id.main_textclock);
        TextView tvTime = findViewById(R.id.time);
        RelativeLayout mRelativeLayout = findViewById(R.id.all);
        Bundle bundle = this.getIntent().getExtras();
        if (bundle != null) {
            String content = bundle.getString("content");
            long time = bundle.getLong("time");
            Date date = new Date(time);
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
            String timeStr = sdf.format(date);
            tvTime.setText(timeStr);
            if (content == null || content.equals("") || content.equals("闹钟")) {
                mainText.setText("时间到了");
                mTTs.playVoice("主人你好,时间到了!主人你好,时间到了!点击任意处可退出闹钟。");
            } else {
                mainText.setText(content);
                int x = 1 + (int) (Math.random() * 2);
                String tip1;
                String tip2;
                if (x == 1) {
                    tip1 = "请记得";
                    tip2 = "哟";
                } else {
                    tip1 = "该";
                    tip2 = "了";
                }
                mTTs.playVoice("主人你好,时间到了!" + tip1 + content + tip2 + ",点击任意处可退出闹钟。");
            }
        }
        mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mClockView.start();
    }

    @Override
    protected void onDestroy() {
        mClockView.stop();
        super.onDestroy();
    }
}
